import $mRoutesIdx from '@/common/router/routes.idx.js'

export default {
	/*
	 * 将data
	 * data.jump_page.code  动态跳转的code 
	 * data.params
	 */
	navigateToIdx(data) {
		console.log(data);
		if (data && data.jump_page) {
			var code = Number(data.jump_page.code);
			var params = data.params;
		}
		console.log(params);
		$mRoutesIdx.navigateToIdx(code, params)

	},

	/*
	 *  params字符串参数中取某个参数的值
	 * 例子：var str = 'a=1&b=2'  =>  getQueryString('a',str)
	 */
	getQueryString(name, query) {
		var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
		var r = query.match(reg);

		if (r != null) {
			return unescape(r[2]);
		}
		return null;
	},

	/* 
	 * obj 转 params字符串参数  
	 * 例子：{a:1,b:2} => a=1&b=2
	 */
	objParseParam(obj) {
		let paramsStr = "";
		if (obj instanceof Array) return paramsStr;
		if (!(obj instanceof Object)) return paramsStr;
		for (let key in obj) {
			paramsStr += `${key}=${obj[key]}&`;
		}
		return paramsStr.substring(0, paramsStr.length - 1);
	},

	/* 
	 * obj 转 路由地址带参数
	 * 例子：{a:1,b:2} => /pages/index/index?a=1&b=2
	 */
	objParseUrlAndParam(path, obj) {
		let url = path || "/";
		let paramsStr = "";
		if (obj instanceof Array) return url;
		if (!(obj instanceof Object)) return url;
		paramsStr = this.objParseParam(obj);
		paramsStr && (url += "?");
		url += paramsStr;
		return url;
	},

	/* 
	 * 获取url字符串参数
	 */
	getRequestParameters(locationhref) {
		let href = locationhref || "";
		let theRequest = new Object();
		let str = href.split("?")[1];
		if (str != undefined) {
			let strs = str.split("&");
			for (let i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
			}
		}
		return theRequest;
	},

	// 10位时间戳转换为时间格式
	timeTen(data) {
		var date = new Date(data)
		var Y = date.getFullYear() + '-'
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
		var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
		var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
		var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
		var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
		return Y + M + D + h + m + s
	},

	// 倒计时

	subTime(stopTimeStr) {
		let stopTime = new Date(stopTimeStr);
		let nowTime = new Date();
		let subTime = Math.floor(stopTime - nowTime) / 1000;
		let days = Math.floor(subTime / 3600 / 24);
		let hours = Math.floor((subTime % (3600 * 24)) / 3600);
		let minutes = Math.floor(((subTime % (3600 * 24)) % 3600) / 60);
		let seconds = Math.floor((subTime % (3600 * 24)) % 3600) % 60;
		days >= 0 && days <= 9 ? days = "0" + days : 0;
		hours >= 0 && hours <= 9 ? hours = "0" + hours : 0;
		minutes >= 0 && minutes <= 9 ? minutes = "0" + minutes : 0;
		seconds >= 0 && seconds <= 9 ? seconds = "0" + seconds : 0;
		return {
			days,
			hours,
			minutes,
			seconds
		}
	}



}
